import * as actions from '../actions/index';

// 页面初始化数据
const initialState = {
  tags: [],
  total: 0,
  currentPage: 1,
  pageSize: 10,
  isLoading: true,
};
const defaultAction = {
  type: 'doNothing',
};

export default function index(state = initialState, action = defaultAction) {
  switch (action.type) {
    case actions.UPDATE_DATA: {
      return { ...state, ...action.data };
    }
    case actions.LOAD_PAGE: {
      const total = action.data.shift().amount;
      return {
        ...state,
        tags: action.data,
        total,
        isLoading: false,
        currentPage: action.data.currentPage,
      };
    }
    case actions.LOADING_DATA: {
      return { ...state, isLoading: true };
    }
    case actions.DELETE_TAG: {
      const tagName = action.data.tag_name;
      const tagList = state.tags.filter((t) => t.name !== tagName);
      return { ...state, tags: tagList };
    }
    default:
      return state;
  }
}
